﻿using System.Net.NetworkInformation;
using System.Text;

namespace ProfileManager.Helpers
{
    public class Network
    {
        public static bool CheckConnection()
        {
            using (Ping pingSender = new Ping())
            {
                PingOptions options = new PingOptions { /*Use the default Ttl value which is 128,*/
                                                          /*but change the fragmentation behavior.*/
                                                          DontFragment = true };
                //Create a buffer of 32 bytes of data to be transmitted.
                const string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                byte[] buffer = Encoding.ASCII.GetBytes(data);

                int successedPing = 0;

                int pingCount = ConfigReader.PingCount;
                for (int i = 0; i < pingCount; i++)
                {
                    PingReply reply = pingSender.Send(ConfigReader.PingServer, ConfigReader.PingTimeout, buffer, options);
                    if (reply != null && reply.Status == IPStatus.Success)
                    {
                        successedPing++;
                    }
                }
                return successedPing > 0;
            }
        }
    }
}
